<?php

defined('BASEPATH') or exit('No direct script access allowed');

/**
 * Description of Areas
 *
 * @author Doufu
 */
class Areas extends Backend_Controller {

	/**
	 * 构造函数
	 */
	public function __construct() {
		parent::__construct();
		$this->isValidRequest();
		// 加载admin模型
		$this->load->model('backend/Areas_model', 'Areas');
		// 加载验证类
		$this->load->library('form_validation');
	}

	/**
	 * 显示所有启用地区
	 */
	public function index() {
		$result = $this->Areas->getAreasAll($this->page, $this->input->post());
		$result ['list'] = $this->Areas->searchAreas(0);
		$this->load->view('backend/areas/index', $result);
	}

	/**
	 * 添加启用地区
	 */
	public function add() {
		if ($this->isPost()) {
			$this->form_validation->validateData(array(
				'address',
				'province_id',
				'city_id',
				'district_id',
				'address_id',
				'type',
				'status'
					), array(
				array(
					'address',
					'required'
				),
				array(
					'province_id',
					'integer'
				),
				array(
					'city_id',
					'integer'
				),
				array(
					'district_id',
					'integer'
				),
				array(
					'address_id',
					'integer|greater_than[0]'
				),
				array(
					'type',
					'integer|in_list[1,2]'
				),
				array(
					'status',
					'integer|in_list[0,10]'
				)
			));

			if ($this->form_validation->hasErrors()) {
				$this->renderJson(300, $this->form_validation->error);
			}

			$data = array(
				'address' => $this->form_validation->address,
				'province_id' => $this->form_validation->province_id,
				'city_id' => $this->form_validation->city_id,
				'district_id' => $this->form_validation->district_id,
				'warehouse_address_id' => $this->form_validation->address_id,
				'type' => $this->form_validation->type,
				'status' => $this->form_validation->status,
				'created_at' => time()
			);
			if (!$this->Areas->is_unique($data)) {
				$this->lang->load('code');
				return $this->renderJson(300, $this->lang->line('e1001'));
			}
			if ($this->Areas->addAreas($data)) {
				$this->renderJson(200, '', array(
					'navTabId' => 'backendareasindex',
					'callbackType' => 'closeCurrent'
				));
			}
			$this->renderJson(300);
		}

		$data ['data'] = $this->Areas->searchAreas(0);
		$this->load->view('backend/areas/add', $data);
	}

	/**
	 * 删除管理员
	 */
	public function delete() {
		$ids = $this->input->post_get('ids');
		if ($this->Areas->deleteAreas($ids)) {
			$this->renderJson(200);
		}

		$this->renderJson(300);
	}

	/**
	 * 修改管理员信息
	 */
	public function edit() {
		if ($this->isPost()) {
			$this->form_validation->validateData(array(
				'id',
				'status'
					), array(
				array(
					'id',
					'integer|greater_than[0]'
				),
				array(
					'status',
					'integer'
				)
			));

			if ($this->form_validation->hasErrors()) {
				$this->renderJson(300, $this->form_validation->error);
			}

			$data = array(
				'status' => $this->form_validation->status
			);
			if ($this->Areas->updateAreas($data, $this->form_validation->id)) {
				$this->renderJson(200, '', array(
					'navTabId' => 'backendareasindex',
					'callbackType' => 'closeCurrent'
				));
			}
			$this->renderJson(300);
		}

		$id = intval($this->get('id'));
		$data = $this->Areas->findAreas($id);
		$this->load->view('backend/areas/edit', $data);
	}

	public function searchAreas() {
		$parent = intval($this->input->post_get('parent'));
		$result = $this->Areas->searchAreas($parent);
		echo json_encode($result);
		die();
	}
}
